import React, { Component } from 'react'
import { NavBar ,SearchBar,WhiteSpace} from "antd-mobile";
import { Getcate, Getgoods } from '../../http/api'
import Left from './components/Left/Left'
import Right from './components/Right/Right'
import"./cate.less"
import {bindActionCreators} from "redux"
import {connect} from "react-redux"
class cate extends Component {
    constructor(){
        super()
        this.state={
            //初始化数据
            //左
            cate:[],
            //右
            catelist:[],
            n:0,
            fid:"",
            idArr:[]
        }
    }
    async changeN(n){
        this.setState({n})

        let res = await Getgoods({
            fid:this.state.cate[n].id
        })

        this.setState({
            catelist:res.data.list?res.data.list:[]
        })
    }

    async componentDidMount(){
        //进来获取信息
        let res = await Getcate()
        this.setState({
            cate:res.data.list?res.data.list:[],
            idArr:res.data.list[0].id
        },()=>{
            let {idArr} = this.state
            Getgoods({
                fid:idArr
            }).then(res=>{
                this.setState({
                    catelist:res.data.list?res.data.list:[]
                })
            })
        })
    }
    render() {
        let {cate,catelist,n}=this.state
        // console.log(catelist);
        return (
            <div>
                <NavBar>商品分类</NavBar>

                <SearchBar placeholder="搜索" maxLength={8} />
                <WhiteSpace />

                <div className="cate">
                    <Left cate={cate} n={n} changeN={this.changeN.bind(this)}></Left>
                    <Right catelist={catelist}></Right>
                </div>
            </div>
        )
    }
}
const mapStateToProps = (state, ownProps) => {
    return {
        
    }
}
const mapDispatchToProps = (dispatch, ownProps) => {
    return {
        // cate:bindActionCreators(actions,dispatch)
    }
}
export default connect(mapStateToProps, mapDispatchToProps)(cate)